package com.github.fancyideas.controller;

import com.github.fancyideas.module.entity.ShoesHisEntity;
import com.github.fancyideas.module.view.ShoesHisView;
import com.github.fancyideas.repository.ShoesHisRepository;
import com.github.fancyideas.repository.view.ShoesHisViewRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "鞋子历史信息", tags = {"客户的鞋子历史信息"})
@RestController
@RequestMapping("/shoeshis")
public class ShoesHisController {

    @Autowired
    private ShoesHisRepository shoesHisRepository;

    @Autowired
    private ShoesHisViewRepository shoesHisViewRepository;

    public void initData() {
        List<ShoesHisEntity> shoesHisEntities = new ArrayList<>();
        int total = 100;
        while (total > 0) {
            String operation = total % 2 == 0 ? "打油" : "保养";
            ShoesHisEntity shoesHisEntity = new ShoesHisEntity();
            shoesHisEntity.setCost(new BigDecimal(total));
            shoesHisEntity.setCustomerId(total + 1L);
            shoesHisEntity.setOperations(operation);
            shoesHisEntity.setPutInAddress("上，下" + total);
            shoesHisEntity.setPutInDate(DateTime.now().plusHours(total).toDate());
            shoesHisEntity.setTakeOutDate(DateTime.now().plusHours(total * 2).toDate());
            shoesHisEntities.add(shoesHisEntity);
            total--;
        }
        shoesHisRepository.saveAll(shoesHisEntities);
    }

    @ApiOperation(value = "生成测试数据", notes = "只有管理员能访问此方法，生产客户信息，每次生成100个")
    @GetMapping("/init")
    public String init() {
        initData();
        return "success";
    }

    @ApiOperation(value = "鞋子历史信息（翻页）", notes = "根据客户Id查询鞋子历史")
    @GetMapping("/page/{pageNo}/{pageSize}")
    public Page<ShoesHisView> listAll(@PathVariable int pageNo,
                                      @PathVariable int pageSize,
                                      @RequestParam(required = false) Long customerId,
                                      @RequestParam(required = false) String putInDate,
                                      @RequestParam(required = false) String takeOutDate) {
        pageNo = pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "customerId");
        Page<ShoesHisView> customerViewPage = shoesHisViewRepository.findAll(
                (Specification<ShoesHisView>) (root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    if (!StringUtils.isEmpty(customerId)) {
                        list.add(criteriaBuilder.equal(root.get("customerId").as(Long.class), customerId));
                    }
                    if (!StringUtils.isEmpty(customerId)) {
                        list.add(criteriaBuilder.equal(root.get("customerId").as(Long.class), customerId));
                    }
                    if (!StringUtils.isEmpty(customerId)) {
                        list.add(criteriaBuilder.equal(root.get("customerId").as(Long.class), customerId));
                    }
                    Predicate[] p = new Predicate[list.size()];
                    return criteriaBuilder.and(list.toArray(p));
                }, pageable);
        return customerViewPage;
    }

}
